﻿using OSGeo.OGR;
using OSGeo.OSR;
using RemoteSensingImageDirectServer.Untils.ScopeSpace;

namespace RemoteSensingImageDirectServer.Untils.Pool
{
    public  class TileResTierPool
    {

        /// <summary>
        /// 私有静态锁对象，减少锁的范围
        /// </summary>
        private static readonly object syncRoot = new();

        /// <summary>
        /// 私有静态变量引用自身实例
        /// </summary>
        private static volatile TileResTierPool? instance = null;

        /// <summary>
        /// 公有静态方法，提供一个全局访问点
        /// </summary>
        /// <returns></returns>
        public static TileResTierPool Instance()
        {
            // 当instance为null时，才进行加锁处理
            if (instance == null)
            {
                // 双重检查锁定，提高性能
                lock (syncRoot)
                {
                    instance ??= new TileResTierPool();
                }
            }
            return instance;
        }

        private TileResTierPool() 
        {
            EnvWGS84 = new RasterEnvelope(-180,-90,180,90);
            EnvGCS2000 = new RasterEnvelope(-180, -90, 180, 90);
            EnvWebMercator = new RasterEnvelope(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892);
        }

        #region 瓦片等级对应分辨率

        /// <summary>
        /// WGS1984坐标系参数
        /// </summary>
        public readonly Envelope EnvWGS84;

        /// <summary>
        /// CGCS2000坐标系参数
        /// </summary>
        public readonly Envelope EnvGCS2000;

        /// <summary>
        /// WebMercator坐标系参数
        /// </summary>
        public readonly Envelope EnvWebMercator;

        /// <summary>
        /// WGS84坐标系下每一级瓦片的分辨率
        /// </summary>
        public  double[] Wgs84Levels { get; private set; } = {
            0.703125,
            0.3515625,
            0.17578125,
            0.087890625,
            0.0439453125,
            0.02197265625,
            0.010986328125,
            0.0054931640625,
            0.00274658203125,
            0.001373291015625,
            0.0006866455078125,
            0.00034332275390625,
            0.000171661376953125,
            0.0000858306884765625,
            0.00004291534423828125,
            0.000021457672119140625,
            0.0000107288360595703125,
            0.00000536441802978515625,
            0.000002682209014892578125,
            0.0000013411045074462890625,
            0.00000067055225372314453125,
            0.000000335276126861572265625,
            0.0000001676380634307861328125,
            0.00000008381903171539306640625
        };

        /// <summary>
        /// Web Mercator坐标系下每一级瓦片的分辨率
        /// </summary>
        public double[] MercatorLevels { get; private set; } ={
            156543.03392800014,
            78271.516963999937,
            39135.758482000092,
            19567.879240999919,
            9783.9396204999593,
            4891.9698102499797,
            2445.9849051249898,
            1222.9924525624949,
            611.49622628137968,
            305.74811314055756,
            152.87405657041106,
            76.437028285073239,
            38.21851414253662,
            19.10925707126831,
            9.5546285356341549,
            4.7773142679493699,
            2.3886571339746849,
            1.1943285668550503,
            0.59716428355981721,
            0.29858214164761665,
            0.149291070823808325,
            0.0746455354119041625,
            0.03732276770595208125,
            0.018661383852976040625
        };

        /// <summary>
        /// 每一级瓦片对应比例尺
        /// </summary>
        public double[] TileScaleDenominator { get; private set; } = {
            559082264.0286474,
            2.795411320143237E8,
            1.3977056600716186E8,
            6.988528300358093E7,
            3.4942641501790464E7,
            1.7471320750895232E7,
            8735660.375447616,
            4367830.187723808,
            2183915.093861904,
            1091957.546930952,
            545978.773465476,
            272989.386732738,
            136494.693366369,
            68247.3466831845,
            34123.67334159225,
            17061.836670796125,
            8530.918335398062,
            4265.459167699031,
            2132.7295838495156,
            1066.3647919247578
        };

        /// <summary>
        /// 超图标准下瓦片的分辨率，单位为米
        /// </summary>
        public double[] SuperMapLevels { get; private set; } = {
            8466.666666666666,
            4233.333333333333,
            2116.6666666666665,
            1058.3333333333333,
            529.1666666666666,
            264.5833333333333,
            132.29166666666666,
            66.14583333333333,
            33.072916666666664,
            16.933333333333334,
            8.466666666666667,
            4.233333333333333,
            2.1166666666666667,
            1.0583333333333333,
            0.5291666666666667,
            0.26458333333333334,
            0.13229166666666667,
            0.06614583333333333,
            0.03307291666666667
        };

        /// <summary>
        /// 超图标准下每级地图的比例尺
        /// </summary>
        public double[] SuperMapTileScaleDenominator { get; private set; } =  {
            32000000,
            16000000,
            8000000,
            4000000,
            2000000,
            1000000,
            500000,
            250000,
            125000,
            64000,
            32000,
            16000,
            8000,
            4000,
            2000,
            1000,
            500,
            250,
            125
        };
        #endregion
    }
}
